package com.senjoeson.teamfriends.adapter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.senjoeson.teamfriends.R;
import com.senjoeson.teamfriends.bean.Friend;
import com.senjoeson.teamfriends.bean.User;
import com.senjoeson.teamfriends.utils.ALog;
import com.senjoeson.teamfriends.utils.GlideUtils;
import com.senjoeson.teamfriends.utils.MapUtils;
import com.senjoeson.teamfriends.utils.MyToast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class FriendListAdapter extends RecyclerView.Adapter {


    private List<Friend> Datas;

    public FriendListAdapter(List<Friend> list) {
        this.Datas = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.myfriend_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        final Friend friend = Datas.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.mNickname.setText(friend.getNickname());
        if ("男".equals(friend.getGender())) {
            viewHolder.mGender.setBackgroundResource(R.mipmap.male);
        } else {
            viewHolder.mGender.setBackgroundResource(R.mipmap.female);
        }
        if (friend.getPortrait() != null) {
            String fileUrl = friend.getPortrait().getFileUrl();
            GlideUtils.loadImage(holder.itemView.getContext(), fileUrl, viewHolder.mPortrait);
        }
        viewHolder.mSign.setText(friend.getSign());
        List<String> hobbies = friend.getHobbies();
        StringBuilder addhobbies = new StringBuilder();
        if (hobbies != null) {
            for (String hobby : hobbies) {
                addhobbies.append(hobby);
            }
        }
        viewHolder.mUserinfo.setText(String.format("%s,%s,%s", friend.getCity(), friend.getSchool(), addhobbies.toString()));
        LatLng other = friend.getLatLng();
        User currentUser = BmobUser.getCurrentUser(User.class);
        final LatLng myUser = currentUser.getLatLng();
        String distance = "0 m";
        if (other != null && currentUser.getLatLng() != null) {
            distance = MapUtils.getLatLngDistance(other, myUser);
        }
        viewHolder.mDistance.setText(distance);
        viewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final AlertDialog.Builder builder = new AlertDialog.Builder(viewHolder.itemView.getContext());
                builder.setTitle("删除好友通知");
                String message = String.format("你确定要删除你的好友(%s)", friend.getNickname());
                builder.setMessage(message);
                builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        friend.delete(new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                    MyToast.showToast("删除成功");
                                    notifyDataSetChanged();
                                } else {
                                    ALog.d(e.getMessage());
                                }
                            }
                        });
                    }
                });

                builder.setNegativeButton("点错了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        builder.create().dismiss();
                    }
                });
                builder.create().show();

               /* String userid = friend.getObjectId();
                BmobQuery<Friend> friendBmobQuery = new BmobQuery<>();
                friendBmobQuery.de(userid, new QueryListener<Friend>() {
                    @Override
                    public void done(Friend friend, BmobException e) {

                    }
                })*/
            }
        });

    }

    @Override
    public int getItemCount() {
        return Datas != null ? Datas.size() : 0;
    }

    public void setData(List<Friend> list) {
        this.Datas = list;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.portrait)
        CircleImageView mPortrait;
        @BindView(R.id.nickname)
        TextView mNickname;
        @BindView(R.id.gender)
        ImageView mGender;
        @BindView(R.id.auth)
        ImageView mAuth;
        @BindView(R.id.userinfo)
        TextView mUserinfo;
        @BindView(R.id.sign)
        TextView mSign;
        @BindView(R.id.distance)
        TextView mDistance;
        @BindView(R.id.delete)
        TextView mDelete;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
